package com.app.aipass.activity.order;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;

import com.app.aipass.BR;
import com.app.aipass.R;
import com.app.aipass.databinding.FragmentOrderRecordBinding;
import com.app.aipass.entity.Message;
import com.app.aipass.fragment.BaseFragmentPagerAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.bus.RxBus;

/**
 * @author: woking
 * @date: 2021/6/7
 * @description：
 */
public class OrderRecordFragment extends BaseFragment<FragmentOrderRecordBinding, BaseViewModel> {

//    private List<Fragment> mFragments;
//    private List<String> titlePager;

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_order_record;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
//        mFragments = pagerFragment(true);
//        titlePager = pagerTitleString(true);
//        //设置Adapter
//        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager);
//        binding.viewPager.setAdapter(pagerAdapter);
//        binding.viewPager.setOffscreenPageLimit(10);
//        binding.tabs.setupWithViewPager(binding.viewPager);
//        binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabs));

        initOrder();
        initGuaDanOrder();

        binding.ivTitleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        binding.tvBuyOrderRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(binding.tvBuyOrderRecord.getTag().equals("false"))
                {
                    binding.tvTitleText.setText("订单记录");
                    binding.tvBuyOrderRecord.setTextColor(getResources().getColor(R.color.app_white_typeface));
                    binding.tvBuyOrderRecord.setTextSize(17);
                    binding.tvBuyOrderRecord.setTag("true");

                    int selectIndex = 0;
                    if(binding.orderViewPager != null)
                    {
                        selectIndex = binding.orderViewPager.getCurrentItem();
                        Log.e("TAG",selectIndex + "=====selectIndex=====111=====message====HTTP==========");
                    }

                    RxBus.getDefault().post(new Message("buy_order",selectIndex));

                    binding.tvSellOrderRecord.setTextColor(getResources().getColor(R.color.app_grey_typeface));
                    binding.tvSellOrderRecord.setTextSize(16);

                    binding.orderLl.setVisibility(View.VISIBLE);
                    binding.guadanOrderLl.setVisibility(View.GONE);
//                    mFragments = pagerFragment(true);
//                    titlePager = pagerTitleString(true);
//                    //设置Adapter
//                    BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager);
//                    binding.viewPager.setAdapter(pagerAdapter);
//                    binding.viewPager.setOffscreenPageLimit(10);
//                    binding.tabs.setupWithViewPager(binding.viewPager);
//                    binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabs));
                }
            }
        });
        binding.tvSellOrderRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(binding.tvBuyOrderRecord.getTag().equals("true"))
                {
                    binding.tvTitleText.setText("挂单记录");
                    binding.tvBuyOrderRecord.setTextColor(getResources().getColor(R.color.app_grey_typeface));
                    binding.tvBuyOrderRecord.setTextSize(16);
                    binding.tvBuyOrderRecord.setTag("false");

                    int selectIndex = 0;
                    if(binding.guadanOrderViewPager != null)
                    {
                        selectIndex = binding.guadanOrderViewPager.getCurrentItem();
                        Log.e("TAG",selectIndex + "=====selectIndex=====111=====message====HTTP==========");
                    }

                    RxBus.getDefault().post(new Message("sell_order",selectIndex));

                    binding.tvSellOrderRecord.setTextColor(getResources().getColor(R.color.app_white_typeface));
                    binding.tvSellOrderRecord.setTextSize(17);

                    binding.orderLl.setVisibility(View.GONE);
                    binding.guadanOrderLl.setVisibility(View.VISIBLE);
//                    mFragments = pagerFragment(false);
//                    titlePager = pagerTitleString(false);
//                    //设置Adapter
//                    BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager);
//                    binding.viewPager.setAdapter(pagerAdapter);
//                    binding.viewPager.setOffscreenPageLimit(10);
//                    binding.tabs.setupWithViewPager(binding.viewPager);
//                    binding.viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.tabs));
                }
            }
        });
    }

    private void initOrder() {
        List<Fragment> mFragments = pagerFragment(true);
        List<String> titlePager = pagerTitleString(true);
        //设置Adapter
        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        binding.orderViewPager.setAdapter(pagerAdapter);
        binding.orderViewPager.setOffscreenPageLimit(10);
        binding.orderTabs.setupWithViewPager(binding.orderViewPager);
        binding.orderViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.orderTabs));
    }

    private void initGuaDanOrder() {
        List<Fragment> mFragments = pagerFragment(false);
        List<String> titlePager = pagerTitleString(false);
        //设置Adapter
        BaseFragmentPagerAdapter pagerAdapter = new BaseFragmentPagerAdapter(getChildFragmentManager(), mFragments, titlePager, FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        binding.guadanOrderViewPager.setAdapter(pagerAdapter);
        binding.guadanOrderViewPager.setOffscreenPageLimit(10);
        binding.guadanOrderTabs.setupWithViewPager(binding.guadanOrderViewPager);
        binding.guadanOrderViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(binding.guadanOrderTabs));
    }

    @Override
    public void initViewObservable() {

    }

    protected List<Fragment> pagerFragment(boolean isOrder) {
        List<Fragment> list = new ArrayList<>();
        list.clear();
        if(isOrder)
        {
            list.add(OrderRecordTabFragment.newInstance(0,isOrder));
            list.add(OrderRecordTabFragment.newInstance(1,isOrder));
            list.add(OrderRecordTabFragment.newInstance(2,isOrder));
            list.add(OrderRecordTabFragment.newInstance(3,isOrder));
            list.add(OrderRecordTabFragment.newInstance(4,isOrder));
            list.add(OrderRecordTabFragment.newInstance(5,isOrder));
            list.add(OrderRecordTabFragment.newInstance(6,isOrder));
        }
        else
        {
            list.add(OrderRecordTabFragment.newInstance(0,isOrder));
            list.add(OrderRecordTabFragment.newInstance(1,isOrder));
            list.add(OrderRecordTabFragment.newInstance(2,isOrder));
            list.add(OrderRecordTabFragment.newInstance(3,isOrder));
            list.add(OrderRecordTabFragment.newInstance(4,isOrder));
        }
        return list;
    }

    protected List<String> pagerTitleString(boolean isOrder) {
        List<String> list = new ArrayList<>();
        list.clear();
        if(isOrder)
        {
            list.add("全部");
            list.add("待付款");
            list.add("待放行");
            list.add("已完成");
            list.add("已取消");
            list.add("异常");
            list.add("待审核");
        }
        else
        {
            list.add("全部");
            list.add("进行中");
            list.add("已完成");
            list.add("已取消");
            list.add("待审核");
        }
        return list;
    }
}